﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yu.Cms.Common;
using System.Data;
using Yu.Enum;

namespace Yu.Cms.Web.UserCtrl.Admin
{
    public partial class Catalog : BaseCtrl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.OnStar();
            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        protected void OnStar()
        {
            this.BindDrpBigType();
            this.BindBigTypeList();
        }

        #region "数据绑定"
        /// <summary>
        /// 绑定大类
        /// </summary>
        protected void BindDrpBigType()
        {
            drpCatalog.DataTextField = "BigTypeName";
            drpCatalog.DataValueField = "BigTypeID";
            drpCatalog.DataSource = bllBigType.GetList();
            drpCatalog.DataBind();
            drpCatalog.Items.Insert(0, new ListItem("请选择父类", "0"));
        }

        /// <summary>
        /// 绑定大类
        /// </summary>
        protected void BindBigTypeList()
        {
            rptBigType.DataSource = bllBigType.GetList();
            rptBigType.DataBind();
        }

        /// <summary>
        /// 绑定小类
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptBigType_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (e.Item.ItemIndex == 0)
                {
                    LinkButton lbtnUp = e.Item.FindControl("lbtnUp") as LinkButton;                    
                    lbtnUp.Enabled = false;
                    lbtnUp.CssClass = "";
                }
                if (e.Item.ItemIndex == bllBigType.GetMaxCount() - 1)
                {
                    LinkButton lbtnDown = e.Item.FindControl("lbtnDown") as LinkButton;
                    lbtnDown.Enabled = false;
                    lbtnDown.CssClass = "";
                }

                Repeater rptSmallType = e.Item.FindControl("rptSmallType") as Repeater;//找到里层的repeater对象
                int BigTypeID = ((Yu.Cms.Model.BigType)e.Item.DataItem).BigTypeID;//找到分类Repeater关联的数据项 
                IList<Yu.Cms.Model.SmallType> lst = bllSmallType.GetList(BigTypeID);
                if (lst.Count > 0)
                {
                    rptSmallType.DataSource = lst;
                    rptSmallType.DataBind();
                }
            }
        }

        protected void rptSmallType_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
             if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (e.Item.ItemIndex == 0)
                {
                    LinkButton lbtnUp = e.Item.FindControl("lbtnUp") as LinkButton;
                    lbtnUp.Enabled = false;
                    lbtnUp.CssClass = "";
                }

                int BigTypeID = ((Yu.Cms.Model.SmallType)e.Item.DataItem).BigTypeID;
                if (e.Item.ItemIndex == bllSmallType.GetMaxCount(BigTypeID)-1)
                {
                    LinkButton lbtnDown = e.Item.FindControl("lbtnDown") as LinkButton;
                    lbtnDown.Enabled = false;
                    lbtnDown.CssClass = "";
                }
            }
        }
        #endregion

        /// <summary>
        /// 添加父栏目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveBig_Click(object sender, EventArgs e)
        {
            Yu.Cms.Model.BigType mBigType = new Yu.Cms.Model.BigType();
            mBigType.BigTypeName = YuRequest.GetFormString(txtBigName.UniqueID);
           bllBigType.Add(mBigType);
            this.OnStar();
        }

        /// <summary>
        /// 添加子栏目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSmallSave_Click(object sender, EventArgs e)
        {
            Yu.Cms.Model.SmallType mSmallType = new Yu.Cms.Model.SmallType();
            mSmallType.BigTypeID = YuRequest.GetFormInt(drpCatalog.UniqueID, 0);
            mSmallType.SmallTypeName = YuRequest.GetFormString(txtSmallName.UniqueID);
           bllSmallType.Add(mSmallType);
            this.OnStar();
        }

        #region "大类操作"
        /// <summary>
        /// 向上移动大类
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void UpBigType(int BigTypeID)
        {
            Yu.Cms.Model.BigType mBigType = bllBigType.GetModel(BigTypeID);
            bllBigType.SortBigType(mBigType, Sort.up);

        }

        /// <summary>
        /// 向下移动大类
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DownBigType(int BigTypeID)
        {
            Yu.Cms.Model.BigType mBigType = bllBigType.GetModel(BigTypeID);
            bllBigType.SortBigType(mBigType, Sort.down);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="BigTypeID"></param>
        protected void UpdateBigType(int ItemIndex,int BigTypeID)
        {
            TextBox txtBigName = rptBigType.Items[ItemIndex].FindControl("txtBigName") as TextBox;
            Yu.Cms.Model.BigType mBigType = bllBigType.GetModel(BigTypeID);
            mBigType.BigTypeName = txtBigName.Text;
            bllBigType.Update(mBigType);
        }

        protected void DelBigType(int BigTypeID)
        {
            if (!bllSmallType.ExistBigTypeID(BigTypeID))
            {
                bllBigType.Delete(BigTypeID);
            }
            else
            {
                JScript.AlertAndGoHistory("子栏目不为空，删除失败");
            }
        }

        /// <summary>
        /// 操作
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void rptBigType_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            string Action = e.CommandName.ToLower().Trim();
            int BigTypeID = TypeConverter.StrToInt(e.CommandArgument.ToString());
            int ItemIndex = e.Item.ItemIndex;
            this.BigTypeOperate(ItemIndex, BigTypeID, Action);
        }

        /// <summary>
        /// 移动/更新/删除 大类
        /// </summary>
        /// <param name="BigTypeID"></param>
        /// <param name="Action"></param>
        protected void BigTypeOperate(int ItemIndex, int BigTypeID, string Action)
        {
            switch (Action)
            {
                case "up":
                    this.UpBigType(BigTypeID);                    
                    break;
                case "down":
                    this.DownBigType(BigTypeID);
                    break;
                case "update":
                    this.UpdateBigType(ItemIndex, BigTypeID);
                    break;
                case "del":
                    this.DelBigType(BigTypeID);
                    break;
                default:
                    break;
            }
            JScript.AlertAndRedirect("操作成功", "Catalog.aspx");
            this.BindBigTypeList();
        }
        #endregion

        #region "小类操作"
        /// <summary>
        /// 向上移动小类
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void UpSmallType(int SmallTypeID)
        {
            Yu.Cms.Model.SmallType mSmallType = bllSmallType.GetModel(SmallTypeID);
            bllSmallType.SortSmallType(mSmallType, Sort.up);
        }

        /// <summary>
        /// 向下移动小类
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DownSmallType(int SmallTypeID)
        {
            Yu.Cms.Model.SmallType mSmallType = bllSmallType.GetModel(SmallTypeID);
            bllSmallType.SortSmallType(mSmallType, Sort.down);
            this.BindBigTypeList();
        }
        
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="BigTypeID"></param>
        protected void UpdateSmallType(int ParentIndex,  int ChildIndex, int SmallTypeID)
        {
            Repeater rptSmallType = rptBigType.Items[ParentIndex].FindControl("rptSmallType") as Repeater;
            TextBox txtSmallName = rptSmallType.Items[ChildIndex].FindControl("txtSmallName") as TextBox;
            Yu.Cms.Model.SmallType mSmallType = bllSmallType.GetModel(SmallTypeID);
            mSmallType.SmallTypeName = txtSmallName.Text;
            bllSmallType.Update(mSmallType);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="BigTypeID"></param>
        protected void DelSmallType(int SmallTypeID)
        {
            if (!bllContent.ExistSmallTypeID(SmallTypeID))
            {
                bllSmallType.Delete(SmallTypeID);
            }
            else
            {
                JScript.AlertAndGoHistory("栏目下还有内容，删除失败");
            }
        }


        /// <summary>
        /// 操作
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void rptSmallType_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            string Action = e.CommandName.ToLower().Trim();
            int SmallTypeID = TypeConverter.StrToInt(e.CommandArgument.ToString());
            RepeaterItem rptItem = e.Item.Parent.Parent as RepeaterItem;
            int ParentIndex = rptItem.ItemIndex;
            int ChildIndex = e.Item.ItemIndex;
            this.SmallTypeOperate(ParentIndex,ChildIndex, SmallTypeID, Action);            
        }
       
        /// <summary>
        ///  移动/更新/删除 小类
        /// </summary>
        /// <param name="ItemIndex"></param>
        /// <param name="SmallTypeID"></param>
        /// <param name="Action"></param>
        protected void SmallTypeOperate(int ParentIndex, int ChildIndex, int SmallTypeID, string Action)
        {
            switch (Action)
            {
                case "up":
                    this.UpSmallType(SmallTypeID);
                    break;
                case "down":
                    this.DownSmallType(SmallTypeID);
                    break;
                case "update":
                    this.UpdateSmallType(ParentIndex, ChildIndex, SmallTypeID);
                    break;
                case "del":
                    this.DelSmallType(SmallTypeID);
                    break;
                default:
                    break;
            }
            JScript.AlertAndRedirect("操作成功", "Catalog.aspx");
            this.BindBigTypeList();
        }
        #endregion



       
        
       
    }
}